﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Xml.Serialization;

namespace Watchdog
{
    public class JsonUtil
    {
        public static string Serialize<T>(T data)
        {
            try
            {
                using (var ms = new MemoryStream())
                {
                    var ser = new DataContractJsonSerializer(typeof(T));
                    ser.WriteObject(ms, data);
                    return Encoding.UTF8.GetString(ms.ToArray());
                }
            }
            catch
            {

            }
            return null;
        }

        public static T Deserialize<T>(string data)
        {
            try
            {
                using (var ms = new MemoryStream(Encoding.Unicode.GetBytes(data)))
                {
                    var serializer = new DataContractJsonSerializer(typeof(T));
                    return (T)serializer.ReadObject(ms);
                }
            }
            catch
            {
                
            }
            return default(T);
        }
    }
}
